package com.headwind.logistics.dispatchNetwork.service.impl;

import com.headwind.logistics.common.pojo.BaseNetSite;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.dispatchNetwork.dto.BaseNetSiteDto;
import com.headwind.logistics.dispatchNetwork.dto.SiteArerTreeDto;
import com.headwind.logistics.dispatchNetwork.mapper.NetworkSiteMapper;
import com.headwind.logistics.dispatchNetwork.service.inter.NetworkSiteInter;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author HUA
 * @Date 2020/7/18 15:17
 * @Version 1.0
 * @DESCRIPTION 网点服务实现层
 */
@Service
public class NetworkSiteImpl implements NetworkSiteInter {

    @Resource
    private NetworkSiteMapper networkSiteDao;

    /**
     * 分页查看所有网点
     * @param siteName
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<BaseNetSite> querySiteAll(String siteName, Integer page, Integer rows){
        if (page != null && rows != null){
            page = (page-1)*rows;
        }

        return networkSiteDao.querySiteAll(siteName,page,rows);
    }

    /**
     * 根据id查询网点
     * @param siteId
     * @return
     */
    @Override
    public BaseNetSite querySiteById(Integer siteId) {
        return networkSiteDao.querySiteById(siteId);
    }

    /**
     * 新增审核网点
     * @param baseNetSiteDto
     * @return
     */
    @Override
    public int addApply(BaseNetSiteDto baseNetSiteDto) {
        return networkSiteDao.addApply(baseNetSiteDto);
    }


    /**
     * 新增网点
     * @param baseNetSiteDto
     * @return
     */
    @Override
    public int addSite(BaseNetSiteDto baseNetSiteDto) {
        UUID uuid = UUID.randomUUID();
        baseNetSiteDto.setRandomNumber(uuid.toString().replace("-", ""));
        baseNetSiteDto.setDistrictId(queryApplyByname(baseNetSiteDto.getCounty()));
        int count = networkSiteDao.addSite(baseNetSiteDto);
        System.out.println(baseNetSiteDto.getDistrictId() +"---------------------------------------------------------------");
        return count;
    }

    /**
     * 根据名字查询行政主键
     * @param name
     * @return
     */
    @Override
    public Long queryApplyByname(String name) {
        return networkSiteDao.queryApplyByname(name);
    }


    /**
     * 树形结构(行政区域)
     * @param id
     * @return
     */
    @Override
    public List<SiteArerTreeDto> getDate(Integer id) {
        List<SiteArerTreeDto> siteArerTreeDtos = new ArrayList<>();

        siteArerTreeDtos = networkSiteDao.getDate(0);


        List<SiteArerTreeDto> siteArerTreeDtoList = networkSiteDao.getDate(id);

        for (SiteArerTreeDto siteArerTreeDto : siteArerTreeDtos) {
            siteArerTreeDto.setBasicData(siteArerTreeDto.getId());
            if (siteArerTreeDto.getId().equals(id)){
                siteArerTreeDto.setChildren(siteArerTreeDtoList);
            }
        }

        return siteArerTreeDtos;
    }

    /**
     * 查询所有员工
     * @return
     */
    @Override
    public List<Employee> queryAllEmployee() {
        return networkSiteDao.queryAllEmployee();
    }

    /**
     * 删除网点
     * @param siteId
     * @return
     */
    @Override
    public int deleteSite(Integer siteId) {
        return networkSiteDao.deleteSite(siteId);
    }




}
